#include "unknownpushbutton.h"

UnknownPushButton::UnknownPushButton(QString NumPath)
{
    this->m_NumPath=NumPath;

    this->setFixedSize(25,25);

    this->m_isTurn =false;

    this->m_guessIsMine = false;

}

void UnknownPushButton::setISMine(bool is)
{
    this->m_isMine=is;
}

void UnknownPushButton::setX(int x)
{
    this->m_posX = x;
}

void UnknownPushButton::setY(int y)
{
    this->m_posY = y;
}

bool UnknownPushButton::getIsMine(void)
{
    return this->m_isMine;
}

int UnknownPushButton::getX(void)
{
    return this->m_posX;
}

int UnknownPushButton::getY(void)
{
    return this->m_posY;
}

bool UnknownPushButton::getIsTurn(void)
{
    return m_isTurn;
}

void UnknownPushButton::setIsTurn(bool is)
{
    this->m_isTurn=is;
}

QString UnknownPushButton::getNumPath(void)
{
    return this->m_NumPath;
}

bool UnknownPushButton::getIsGuess(void)
{
    return this->m_guessIsMine;
}

void UnknownPushButton::turnBtn(void)
{
    QPixmap pix;
    pix.load(this->m_NumPath);
    pix = pix.scaled(this->width(),this->height());
    this->setIcon(pix);
    this->m_isTurn=true;
}

void UnknownPushButton::mousePressEvent(QMouseEvent *e)
{
    if(e->button() & Qt::LeftButton)
    {
        if(this->m_isTurn==true||this->m_guessIsMine == true)
            return;

        this->turnBtn();
        //如果按钮值为0，发出zero信号
        if(this->m_NumPath==":/unknown/res/0.png")
        {
            emit this->isZero();
        }
        //如果按钮为雷发出失败信号
        if(this->m_isMine==true)
        {
            emit this->youLose();
        }
    }
    if(e->button() & Qt::RightButton)
    {
        if(this->m_isTurn==true)
            return;
        if(this->m_guessIsMine == true)
        {
            this->m_NumPath = this->m_tempNumPath;
            this->setIcon(QIcon());
        }
        else if(this->m_guessIsMine == false)
        {
            this->m_tempNumPath = this->m_NumPath;
            this->m_NumPath = ":/res/funny.png";
            QPixmap pix;
            pix.load(this->m_NumPath);
            pix = pix.scaled(this->width(),this->height());
            this->setIcon(pix);
        }
        this->m_guessIsMine=!this->m_guessIsMine;
        emit this->isGuess();
    }
}
/********************点击开始键时更新按钮****************************/
void UnknownPushButton::update(Board *map)
{
    this->m_isTurn = false;
    this->m_guessIsMine = false;
    this->m_isMine=map->isMine(m_posX,m_posY);

    int i = this->m_posX;
    int j = this->m_posY;

    QString path;

    if(map->isMine(i,j)==true)
    {
        path = ":/res/bomb.png";
    }
    else
    {
        int mineNum=0;//统计周围雷数
        if(i==0&&j==0)
            mineNum=map->isMine(i+1,j)+map->isMine(i+1,j+1)+map->isMine(i,j+1);
        else if(i==0&&j==9)
            mineNum=map->isMine(i+1,j)+map->isMine(i+1,j-1)+map->isMine(i,j-1);
        else if(i==0)
            mineNum=map->isMine(i,j-1)+map->isMine(i,j+1)+map->isMine(i+1,j+1)+map->isMine(i+1,j)+map->isMine(i+1,j-1);
        else if(i==9&&j==0)
            mineNum=map->isMine(i-1,j)+map->isMine(i-1,j+1)+map->isMine(i,j+1);
        else if(i==9&&j==9)
            mineNum=map->isMine(i-1,j)+map->isMine(i-1,j-1)+map->isMine(i,j-1);
        else if(i==9)
            mineNum=map->isMine(i,j-1)+map->isMine(i,j+1)+map->isMine(i-1,j+1)+map->isMine(i-1,j)+map->isMine(i-1,j-1);
        else if(j==0)
            mineNum=map->isMine(i-1,j)+map->isMine(i+1,j)+map->isMine(i-1,j+1)+map->isMine(i,j+1)+map->isMine(i+1,j+1);
        else if(j==9)
            mineNum=map->isMine(i-1,j)+map->isMine(i+1,j)+map->isMine(i-1,j-1)+map->isMine(i,j-1)+map->isMine(i+1,j-1);
        else
            mineNum=map->isMine(i-1,j-1)+map->isMine(i-1,j)+map->isMine(i-1,j+1)
                    +map->isMine(i,j-1)+map->isMine(i,j+1)
                    +map->isMine(i+1,j-1)+map->isMine(i+1,j)+map->isMine(i+1,j+1);

        //设置按钮图标路径
        path = ":/unknown/res/"+QString::number(mineNum)+".png";
    }
    this->m_NumPath=path;

    //设置图标为空
    this->setIcon(QIcon());
}

